package cn.wolfcode.luowowo2.util;

import cn.wolfcode.luowowo2.common.util.Consts;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Albert on 2019/7/6.
 */
public class CookieUtil {
    public static void addCookie(HttpServletResponse response, String name, String value) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath("/");
        cookie.setMaxAge(Consts.USER_INFO_TOKEN_VAL_TIME * 60);
        response.addCookie(cookie);
    }


    public static Cookie getCookie(HttpServletRequest request, String cookieName) {
        Cookie cookie = null;
        Cookie[] allCookies = request.getCookies();
        if (allCookies == null) {
            return null;
        }
        for (Cookie gcookie : allCookies) {
            if (cookieName.equals(cookieName)) {
                cookie = gcookie;
            }
        }

        return cookie;
    }
}
